﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class UserDelConfirmForm : Form
    {
        MONITOR_DBDataContext dbcontext = new MONITOR_DBDataContext();
        string userid;
        
        public UserDelConfirmForm(string id)
        {
            InitializeComponent();
            userid = id;
        }

        private void button1_Click(object sender, EventArgs e)
        {
              tb_user item = dbcontext.tb_user.Single(c => c.ID == userid);
              dbcontext.tb_user.DeleteOnSubmit(item);

              string[] deletedata = new string[11];


              deletedata[0] = item.ID.ToString();
              deletedata[1] = item.Name;
              deletedata[2] = item.sex;
              deletedata[3] = item.Role;
              deletedata[4] = item.IDcard == null ? "无" : item.IDcard;
              deletedata[5] = item.Birthday == null ? "无" : item.Birthday.ToString();
              deletedata[6] = item.Phone == null ? "无" : item.Phone;
              deletedata[7] = item.email == null ? "无" : item.email;
              deletedata[8] = item.Workaddress == null ? "无" : item.Workaddress;
              deletedata[9] = item.Homeaddress == null ? "无" : item.Homeaddress;
              deletedata[10] = item.Comment == null ? "无" : item.Comment;

              string detail = UserLog.builddetailstring(UserLog.usermanagerheader, "删除", deletedata, null);

              try
              {
                  tb_userauth item1 = dbcontext.tb_userauth.Single(c => c.userID == userid);

                  if (item1 != null)
                  {
                      dbcontext.tb_userauth.DeleteOnSubmit(item1);
                  }

                  dbcontext.SubmitChanges();
                  UserLog.addusermanagerecord(User.UserID, User.Username, "删除", item.ID, item.Name, "成功", detail);


                  MessageBox.Show("删除用户成功");
              }
              catch (Exception ex)
              {
                  UserLog.addusermanagerecord(User.UserID, User.Username, "删除", item.ID, item.Name, "失败", detail);
                  MessageBox.Show("删除用户失败");
              }
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            

        }
    }
}
